﻿.L, Start,, implement later ability to restart
,,,
,,, Global Values Do not change
,,, %s0 = balls direction
,,,
movi, $7, %s0, left-down

,,,
,,, Intialize Game Setup Values/registers
,,, Can extract to use to test hardware
,,,
movi, $126, %ballx
movi, $126, %bally
movi, $32, %p1_paddle
movi, $32, %p2_paddle
movi, $0, %p1_score
movi, $0, %p2_score
movi, $0, %p1_input
movi, $0, %p2_input


.L, BeginGame,, REPEATING FUNCTIONS: No variable preintializations that are dynamicly set

,,, Setup initial variables for paddle movement
,,,
,,, %s3 Lowerbound = 384
,,, Const: Upperbound = 0
,,, %s4 Speed = 2
,,,
,,,if(IsUp) --> !IsUpcollision --> T: MoveUp F: P1_End
,,,else if(isDown) --> !isDowncollision --> T: MoveDown F: P1_End
,,,else P1_END
 
,,,
,,, P1 paddle movement/collision
,,,

.L,    P1_input,,                  player 1 paddle
mov,   %p1_input,  %s1,
mov,   %s1,  %s2,
movi, $96, %s3,                    Put 96 into s3 to create 96<<2 = 384, lower bound of map
lshi,  $-2, %s3,                    96<<4 = 384 = s3
movi, $1, %S4,                     s4 = paddles speed must be in increments of 2 or equal to 1
 
,,,           
,,, Check for users input & collision
,,, 1) Masks input for up/down bit
,,, 2) Check upper/lower bound of map with current paddle position, if ok move paddle
,,, --My code flows sequentially instead of using jal/jr
 
 
,,,
,,, IsUp()
,,,
 
.L, P1_IsUp,,                  P1 IsUP()
movi, $1, %s6
lshi, $-7, %s6
and, %s6, %s1,             mask p1 input w/ up bit = s6
cmpi,     $0, %s1,                      
jei, &P1_IsDown,,               if 0: its not set
 
,,,
,,, IsUpCollision(): IsUp() has been set by player 1: Check bounds
,,,
 
cmpi, $0,  %p1_paddle,     p1_paddle == upperbound of map
jei,          &P1_End,,    p1_paddle == upperbound else
 
,,, MoveUp()
,,,
.L,   P1_MoveUp
sub,  %s4, %p1_paddle
j,    &P1_End,,            Finished moving up end p1 movement/collision detection cannot have more than 1 button
 
,,,
,,, IsDown()
,,,
 
.L, P1_IsDown,,
movi, $1, %s6
lshi, $-6, %s6                     
and, %s6, %s2,       mask p1 input w/ up bit
cmpi, $0, %s2,                      
jei,  &P1_End,,            if 0, its not set
 
,,,
,,, IsDownCollision: IsDown() has been set by player 1: Check bounds
,,,
 
cmp, %s3, %p1_paddle,     p1_paddle == lowerbound of map
jei,  &P1_End,,            p1_paddle == lowerbound do not move paddle
 
,,, MoveDown()
,,,
.L,  P1_MoveDown
add, %s4, %p1_paddle
 
.L, P1_End
,,,
,,, End of P1 paddle movement
,,,

,,,
,,, Moving Ball Instructions
,,,

,,,
,,, Setup Initial Variables
,,,
,,, ball hit paddle collion value = 32: 32 Leftside | (640-32) Rightside
,,, score width from edge of screen = 16: 16 Leftside | (640-16) Rightside
,,,
,,, %S0 = balls moving direction
,,, %s1 = screen rightside collison value (screen - paddle width) = 608 = 76<<3
,,, %s2 = screen lowerbound collison value (screen max) = 448 = 112<<2
,,, %s3 = score width from edge of screen 
,,, %s4 = ball speed

movi, $76, %s1
lshi, $-3, %s1, s1 = 76<<3

movi, $112, %s2
lshi, $-2, %s2, s2 = 112<<2

movi, $16, %s3
movi, $1, %s4

,,, %s0
,,, 0 Right
,,, 1 Left
,,, 2 Right Up
,,, 3 Left Up
,,, 6 Right Down (keep low bits the same as the right up)
,,, 7 Left Down
,,,

,,,
,,, Collision Detection for ball
,,,
.L, Collision,,
cmpi, $32, %ballx, ball hit player one
jei, &player1
cmp, %s1, %ballx, ball hit player two
jei, &player2
cmpi, $0, %bally, ball hit celling
jei, &Wall
cmp, %s2, %bally, ball hit floor
jei, &Wall
.L, Wall
xori, $4, %s0, flip direction down/up
j, &end
.L, player2,, set movement left
movi, $1, %s0, change direct to left
j, &end
.L, player1,, set movement right
movi, $0, %s0, change direct to right
.L, end
,,, %s0
,,, 0 Right
,,, 1 Left
,,, 2 Right Up
,,, 3 Left Up
,,, 6 Right Down (keep low bits the same as the right up)
,,, 7 Left Down
,,,

.L, direction
cmpi, $0, %S0, right
jei, &right
cmpi, $1, %s0, left
jei, &left
cmpi, $2, %s0, right up
jei, &rightUP
cmpi, $3, %s0, left up
jei, &leftUP
cmpi, $6, %s0, right down
jei, &rightDOWN
cmpi, $7, %s0, left down
jei, &leftDOWN
.L, rightDOWN
add, %s4, %ballx, down and to the right
add, %s4, %bally
j, &endBall
.L, leftUP
sub, %s4, %ballx, up and to the left
sub, %s4, %bally
j, &endBall
.L, leftDOWN
sub, %s4, %ballx, down and to the left
add, %s4, %bally
j, &endBall
.L, rightUP
add, %s4, %ballx, up and to the right
sub, %s4, %bally
j, &endBall
.L, left
sub, %s4, %ballx, straight left
j, &endBall
.L, right
add, %s4, %ballx, straight right
.L, endBall

,,,
,,, End Ball()
,,,

,,,
,,, Prevent game from going to fast, Refresh Rate Loop = 500
,,, s5 will equal 32766.... this will cause it to wait about 5 ms ish
,,,wait, $100

movi, $127, %s5
lshi, $-7, %s5
addi, $127, %s5
lshi, $-1, %s5

.L, wait
nop
nop
subi, $1, %s5
cmpi, $0, %s5
jei, &wait2Begin
j, &wait
,,,2x

.L, wait2Begin
movi, $127, %s5
lshi, $-7, %s5
addi, $127, %s5
lshi, $-1, %s5

.L, wait2
nop
nop
subi, $1, %s5
cmpi, $0, %s5
jei, &BeginGame
j, &wait2,, REPEAT MOVEMENT...